<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Marko B
 * Date: 5/24/13
 * Time: 1:23 PM
 * To change this template use File | Settings | File Templates.

/*
$username="sitmonte_ivan";
$password="print00";
$hostname="mysql";
*/

require_once('constants.php');

//connection to the database
$dbhandle=mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");


mysql_set_charset('utf8');
//select a database to work with
$selected=mysql_select_db($database, $dbhandle)
or die("Could not select examples");

if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comment'])  && isset($_POST['post_ID']))
{


    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);
    $comment = mysql_real_escape_string($_POST['comment']);
    $post_ID = mysql_real_escape_string($_POST['post_ID']);

    $q = "INSERT INTO comments
    (posts_ID, comment, email, name, date)
    VALUES (" . $post_ID . ",'" . $comment . "'" . ",'" . $email . "'"  . ",'" . $name . "'" . ",'" . time() . "')";

    $success=false;

    if(mysql_query($q))
        $success = true;
    if($success)
    {
        echo json_encode(array('action' => true, 'msg' => 'Uspješan unos komentara. '));
    }
    else
    {
        echo json_encode(array('action' => false, 'msg' => 'Greška prilikom slanja komentara. Molimo pokušajte ponovo.'));
    }
}
else
{
    echo json_encode(array('action' => false, 'msg' => 'Greška prilikom slanja komentara. Molimo popunite sva polja i pokušajte ponovo.'));
}

mysql_close($dbhandle);